/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-27 17:44:45
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-19 14:49:32
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import {
    StorePutModelContentDto, StoreFunctionsServiceProxy, StorePutServiceProxy, StoreInfoServiceProxy, ReleaseOrderDto,
    ReleaseOrderInputs,
    StoreDropListOutDto
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import * as moment from 'moment';
@Component({
    selector: 'app-supplementary-certificate',
    templateUrl: './supplementary-certificate.component.html',
    styleUrls: ['./supplementary-certificate.component.less']
})
export class SupplementaryCertificateComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    saving = false;
    active = false;
    form = new StorePutModelContentDto();
    model = [];
    sendTime = [];
    modelOptions = [];
    sendTimeOptions = [];
    materialsNodeOptions = [];
    moduleOptions = [];
    unitOptions = [];
    materialCategoryOptions = [];
    binLocationOptions = [];
    releaseOrderOptions = [];// 放行单下拉
    tableShowForm = new ReleaseOrderDto();//合格证
    materialProperties: Boolean = true;//0=true=正常品  ------------ 2=false=返修品
    searched: boolean = false;
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
        private _storeInfoServiceProxy: StoreInfoServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
    }
    onChange(event) {
        if (this.form.materialsNodeId != undefined) {
            //物料细类
            this._storeFunctionsServiceProxy.getSegmentationMaterialsTypeDropList(
                this.form.materialsNodeId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.materialCategoryOptions = [];
                    this.materialCategoryOptions = result;
                });
        }
    }
    save() {
        let input = new ReleaseOrderInputs();
        for (let i = 0; i < this.model.length; i++) {
            let item = this.model[i];
            if (i == 0 && this.model.length - 1 != 0) {
                this.form.productCode = item.value + ',';
            } else if (i == 0 && this.model.length - 1 == 0) {
                this.form.productCode = item.value;
            } else if (i == this.model.length - 1) {
                this.form.productCode = this.form.productCode + item.value;
            } else {
                this.form.productCode = this.form.productCode + item.value + ',';
            }
        }
        for (let i = 0; i < this.sendTime.length; i++) {
            let item = this.sendTime[i];
            if (i == 0 && this.sendTime.length - 1 != 0) {
                this.form.sendTime = item.value + ',';
            } else if (i == 0 && this.sendTime.length - 1 == 0) {
                this.form.sendTime = item.value;
            } else if (i == this.sendTime.length - 1) {
                this.form.sendTime = this.form.sendTime + item.value;
            } else {
                this.form.sendTime = this.form.sendTime + item.value + ',';
            }
        }
        for (const item of this.materialsNodeOptions) {
            if (item.value == this.form.materialsNodeId) {
                this.form.materialsNode = item.label;
            }
        }
        for (const item of this.materialCategoryOptions) {
            if (item.value == this.form.materialCategoryId) {
                this.form.materialCategory = item.label;
            }
        }
        for (const item of this.unitOptions) {
            if (item.value == this.form.unitId) {
                this.form.unit = item.label;
            }
        }
        if (this.materialProperties) {
            this.form.materialsProperty = 0;
        } else {
            this.form.materialsProperty = 1;
        }
        for (const item of this.binLocationOptions) {
            if (item.value == this.form.binLocationId) {
                this.form.binLocation = item.label;
            }
        }
        input.storePutModelContentDto = [];
        this.form.id = "00000000-0000-0000-0000-000000000000";
        input.storePutModelContentDto.push(this.form);
        input.releaseOrderDto = this.tableShowForm;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._storeInfoServiceProxy.supplementaryCertificate(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((rs) => {
                    this.saveFlag = true;
                    if (rs.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                    } else {
                        this._messageService.error(rs.message);
                    }
                });
        }

    }
    onTabChange(event) {
        this.tableShowForm.expirationDate = moment(this.tableShowForm.expirationDate).format('YYYY-MM-DD');
    }
    search(type) {
        if (type == 1) {
            this._storePutServiceProxy.getCertificateInfoByCerCode(
                this.form.qualifiedNumber
            ).pipe(finalize(() => { }))
                .subscribe(rs => {
                    this.form = rs;
                    //规格
                    this._storeFunctionsServiceProxy.getMaterialsContentsModelList(
                        this.form.productDrawingNo
                    ).pipe(finalize(() => { }))
                        .subscribe(result => {
                            this.moduleOptions = [];
                            this.moduleOptions = result;
                        });
                    this.model = [];
                    if (rs.productCode != null) {
                        let model = rs.productCodeName.split(',');
                        let modelValue = rs.productCode.split(',');
                        let modelData = [];
                        for (let i = 0; i < model.length; i++) {
                            const item = model[i];
                            let data = new StoreDropListOutDto();
                            data.label = item;
                            data.value = modelValue[i];
                            modelData.push(data);
                        }
                        // this.searched = false;
                        //型号（产品代号）
                        this._storeFunctionsServiceProxy.getRocketModeDropList(
                        ).pipe(finalize(() => { }))
                            .subscribe(result => {
                                this.modelOptions = [];
                                this.modelOptions = result;
                                this.model = modelData;
                                this.getSendTime(1);
                                // this.searched = true;
                            });
                    };
                    if (rs.materialCategoryId != null) {
                        this._storeFunctionsServiceProxy.getSegmentationMaterialsTypeDropList(
                            this.form.materialsNodeId
                        ).pipe(finalize(() => { }))
                            .subscribe(result => {
                                this.materialCategoryOptions = [];
                                this.materialCategoryOptions = result;
                                this.form.materialCategoryId = rs.materialCategoryId;
                            });
                    }
                });
        } else if (type == 2) {
            //放行单
            this._storeInfoServiceProxy.getReleaseOrderContentList(
                this.tableShowForm.id
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    let id = this.tableShowForm.id;
                    this.tableShowForm = result;
                    for (const item of this.releaseOrderOptions) {
                        if (id == item.value) {
                            this.tableShowForm.releaseOrder = item.label;
                        }
                    }
                });
        }
    }
    //打开弹窗
    onShown(): void {
        this.active = true;
        //单位
        this._storeFunctionsServiceProxy.getCodeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.unitOptions = [];
                this.unitOptions = result;
            });
        //型号（产品代号）
        this._storeFunctionsServiceProxy.getRocketModeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.modelOptions = [];
                this.modelOptions = result;
            });
        //物料类别
        this._storeFunctionsServiceProxy.getMaterialsTypeDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialsNodeOptions = [];
                this.materialsNodeOptions = result;
            });
        //储位
        this._storeFunctionsServiceProxy.getBinLocationDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.binLocationOptions = [];
                this.binLocationOptions = result;
            });
        //放行单下拉
        this._storeInfoServiceProxy.getReleaseOrder(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.releaseOrderOptions = [];
                this.releaseOrderOptions = result;
            });
    }
    getSendTime(event) {
        //发次
        this._storeFunctionsServiceProxy.getRocketLotNoDropList(
            this.model
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
            });
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form = new StorePutModelContentDto();
        this.tableShowForm = new ReleaseOrderDto();
        this.materialCategoryOptions = [];
        this.model = [];
        this.sendTime = [];
        this.active = false;
        this.modalSave.emit();
        this.modal.hide();
    }
}
